using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration ;
using System.Web.UI;
using System.Web.UI.WebControls;
 

public partial class Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ControlHelper controlHelper;
        try
        {
            string connectionString =
                    WebConfigurationManager.ConnectionStrings[
                    "InterShop"].ConnectionString;
            string itemID = Request.QueryString.Get("ItemID");
            if (itemID != "" && itemID != null )
            {        
                controlHelper = new
                ControlHelper(connectionString);
                controlHelper.CommandText =
                "select I.ItemID, ISBN, " +
                " ItemName, SalesPrice from " +
                " tblItems I, tblItemDetails D" +
                " where I.ItemID=D.ItemID and "+
                " I.ItemID =@ItemID";                         
                controlHelper.CommandType =
                CommandType.Text;
                controlHelper.ParameterCollection = new string[1] { "@ItemID" };
                controlHelper.ValueCollection= new string[1] {itemID};
                controlHelper.InitProperties();
                controlHelper.FillDataTableToDetailsView(this.DetailsView1);
                btnAdd.Enabled = true;
                this.btnCheckOut.Enabled = true;             
            }
            else
            {
                Literal1.Text ="Item not found";
                btnAdd.Enabled = false;
                this.btnCheckOut.Enabled = false;             
            }
         }
         catch(Exception ex)
            {
                Literal1.Text =ex.Message;
            }
        
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        DataRowView dataRowView = 
        (DataRowView)DetailsView1.DataItem;
        Literal1.Text = "You just added \"" 
        + dataRowView[2].ToString() 
        + "\" to Shopping cart";
        AddToShoppingCart(
        dataRowView[0].ToString(), 
        dataRowView[2].ToString(), 
        dataRowView[3].ToString());
    }
    void AddToShoppingCart(string itemID, string itemName, string itemPrice)
    {
        DataTable dataTable;
        if (Session["ShoppingCart"] == null)
        {
            dataTable = new DataTable();
            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Quantity");
            dataTable.Columns.Add("Price");
        }
        else
        {
            dataTable = (DataTable)Session["ShoppingCart"];
        }
        Common cls = new Common();
        int indexOfItem = cls.IsExistItemInShoppingCart(itemID, dataTable);
        if (indexOfItem != -1)
        {
            dataTable.Rows[indexOfItem]["Quantity"] =
            Convert.ToInt32(
            dataTable.Rows[indexOfItem]["Quantity"]) + 1;
        }
        else
        {
            DataRow dataRow = dataTable.NewRow();
            dataRow["ID"] = itemID;
            dataRow["Name"] = itemName;
            dataRow["Quantity"] = "1";
            dataRow["Price"] = itemPrice;
            dataTable.Rows.Add(dataRow);
        }
        Session["ShoppingCart"] = dataTable;
    }
}
